package sort;
import java.util.Random;

public class BubleSort {

	public static void sort(int[] toSort) {
		boolean swapped = false;

		do {
			swapped = false;

			for (int i = 0; i + 1 < toSort.length; i++) {
				if (toSort[i] > toSort[i + 1]) {
					swap(toSort, i, i + 1);
					swapped = true;
				}
			}
		} while (swapped);

	}

	private static void swap(int[] toSort, int i, int j) {
		int temp = toSort[i];
		toSort[i] = toSort[j];
		toSort[j] = temp;
	}

	public static void main(String[] args) {
		Random random = new Random();
		int length = random.nextInt(10);
		int[] toSort = new int[length];
		for (int i = 0; i < length; i++) {
			toSort[i] = random.nextInt(10);
		}
		BubleSort.sort(toSort);

		for (int i = 0; i < toSort.length; i++) {
			System.out.print(toSort[i] + " ");
		}
	}
}
